package gold.digger;

import gold.utils.InputUtil;
import gold.vo.TreeNode;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC894 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<TreeNode> allPossibleFBT(int n) {
            return allPossibleFBTRecur(n);
        }

        public List<TreeNode> allPossibleFBTRecur(int n) {
            if (n % 2 == 0) return new ArrayList<>();
            if (n == 1) return Arrays.asList(new TreeNode(0));

            // 循环遍历当前left子树的节点数量
            List<TreeNode> res = new ArrayList<>();
            for (int i = 1; i < n - 1; i += 2) {
                List<TreeNode> leftPart = allPossibleFBTRecur(i);
                List<TreeNode> rightPart = allPossibleFBTRecur(n - i - 1);
                for (TreeNode leftNode : leftPart) {
                    if (rightPart.size() <= 0) {
                        TreeNode curRoot = new TreeNode(0);
                        curRoot.left = leftNode;
                        res.add(curRoot);
                    } else {
                        for (TreeNode rightNode : rightPart) {
                            TreeNode curRoot = new TreeNode(0);
                            curRoot.left = leftNode;
                            curRoot.right = rightNode;
                            res.add(curRoot);
                        }
                    }
                }
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.allPossibleFBT(7));
    }

    public static void main(String[] args) throws Exception {
        LC894 an = new LC894();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
